<template>
  <div>
    <template v-if="listData.length > 0">
      <el-row :gutter="24">
        <el-col class="mb20" v-for="(item,index) in listData" :key="index" :span="8">
          <el-card shadow="never" class="list-card">
            <div slot="header" class="tl-center">
              <span>{{item.evalPlanName}}</span>
              <el-tooltip class="item" effect="dark" content="详情" placement="top">
                <i
                  @click.stop="handleDetail(item)"
                  v-if="$auth('eval:detail')"
                  class="fr curp fs16 el-icon-document"
                />
              </el-tooltip>
              <el-tooltip class="item" effect="dark" content="复制新增" placement="top">
                <i
                  @click.stop="handleEdit(true, item)"
                  v-if="$auth('eval:add')"
                  class="fr curp mr10 fs16 el-icon-folder"
                />
              </el-tooltip>
              <el-tooltip class="item" effect="dark" content="删除" placement="top">
                <i
                  v-if="$auth('eval:delete')"
                  @click.stop="handleDelete(item)"
                  class="red mr10 fr curp fs16 el-icon-delete"
                />
              </el-tooltip>
            </div>
            <el-form label-width="110px" label-suffix="：">
              <el-form-item label="任务周期">
                <span>{{ judgeRange(item.evalPlanCycle) }}</span>
              </el-form-item>
              <el-form-item label="任务日期">
                <span>{{ item.startDate }}{{ item.endDate ? `至${item.endDate}` : null }}</span>
              </el-form-item>
              <el-form-item label="创建人">{{ item.createUser }}</el-form-item>
              <el-form-item label="创建时间">{{ formatTime(item.createTime) }}</el-form-item>
              <el-form-item label="考评形式">{{ item.evalMode === 1 ? '填报' : '巡查' }}</el-form-item>
              <el-form-item label="任务状态">
                <span>
                  <el-tag :type="judgeType(item.state)">{{ judgeState(item.state) }}</el-tag>
                </span>
                <el-tooltip
                  v-if="item.state === 2"
                  class="item"
                  effect="dark"
                  content="作废"
                  placement="top"
                >
                  <i
                    @click.stop="handleChangeState(item)"
                    class="ml10 curp red fs16 el-icon-error"
                  />
                </el-tooltip>
                <el-tooltip
                  v-if="item.state === 1"
                  class="item"
                  effect="dark"
                  content="启用"
                  placement="top"
                >
                  <i
                    @click.stop="handleChangeState(item)"
                    class="ml10 curp fs16 el-icon-video-play"
                  />
                </el-tooltip>
              </el-form-item>
              <el-form-item label="延期天数">{{ item.delay ? item.delay : 0 }}</el-form-item>
              <el-form-item label="执行次数">{{ item.evalPlanCycleNum }}</el-form-item>
              <!-- <el-form-item label="执行者形式">{{ item.executorMode === 1 ? '指定部门' : '指定个人' }}</el-form-item> -->
              <el-form-item
                label="考核对象形式"
              >{{ item.targetMode === 1 ? '指定部门' : ( item.targetMode === 2 ? '指定岗位' : '指定人员' ) }}</el-form-item>
            </el-form>
          </el-card>
        </el-col>
        <el-col class="tl-center nomoreWord" v-show="noMore">---- 这是我的底线 ----</el-col>
      </el-row>
    </template>
    <template v-else>
      <empty-comp ref="emptyComp" />
    </template>
  </div>
</template>

<script>
import { openNotification, judgeRange, judgeEvalState, judgeType } from '@/utils/util'
import { planQuery, planUpdate, planDelete } from '@/api/eval'
export default {
  data() {
    return {
      listData: [],
      total: 1,
      param: {
        pageSize: 10,
        pageNo: 1
      },
      noMore: false
    }
  },
  mounted() {
    this.getPlanList()
    window.addEventListener('scroll', this.getScroll)
  },
  destroyed() {
    window.removeEventListener('scroll', this.getScroll)
  },
  methods: {
    getScroll() {
      if (document.documentElement.scrollTop + document.documentElement.clientHeight >= document.documentElement.scrollHeight) {
        this.loadMore()
      }
    },
    loadMore() {
      console.log('loadMore', this.listData.length, this.total)

      if (this.param.pageNo * 10 > this.total) {
        this.noMore = true
      } else {
        this.param.pageNo += 1
        this.getPlanList(this.param)
      }
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.evalPlanName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await planDelete(row.evalPlanId, {})
          openNotification('success', '删除成功')
          const index = _this.listData.findIndex(v => v.evalPlanId === row.evalPlanId)
          _this.listData.splice(index, 1)
          // _this.handleGetList()
        }
      })
    },
    getPlanList(param = {}) {
      this.param = Object.assign(this.param, param)
      planQuery(this.param).then(res => {
        console.log('planQuery', res)
        if (res && res.data.records.length > 0) {
          if (this.param.pageNo === 1) {
            this.listData = []
            this.listData = [...res.data.records]
          } else {
            this.listData = [...this.listData, ...res.data.records]
          }
          this.total = res.data.total
          if (this.param.pageNo * 10 > this.total) {
            this.noMore = true
          }
        } else {
          this.listData = []
          this.total = 1
        }
      })
    },
    judgeType(num) {
      return judgeType(num)
    },
    judgeState(num) {
      return judgeEvalState(num)
    },
    judgeRange(num) {
      return judgeRange(num)
    },
    async handleChangeState(item) {
      const _this = this
      if (item.state === 2) {
        this.$confirm({
          title: '作废操作',
          content: `您确定作废[${item.evalPlanName}]这条记录吗？`,
          okText: '确定',
          okType: 'danger',
          cancelText: '取消',
          centered: true,
          async onOk() {
            // 调用删除接口，刷新表格数据
            const res = await planUpdate({ evalPlanId: item.evalPlanId, state: 4 })
            if (res.state === '000000') {
              openNotification('success', '作废操作成功')
              const index = _this.listData.findIndex(v => v.evalPlanId === item.evalPlanId)
              _this.$set(_this.listData[index], 'state', 4)
            }
          }
        })
      } else if (item.state === 1) {
        const res = await planUpdate({ evalPlanId: item.evalPlanId, state: 2 })
        if (res.state === '000000') {
          openNotification('success', '启用操作成功')
          const index = _this.listData.findIndex(v => v.evalPlanId === item.evalPlanId)
          _this.$set(_this.listData[index], 'state', 2)
          // _this.getPlanList()
        }
      }
    },
    handleDetail(row) {
      this.$router.push({
        path: '/evaluate/evaluate-planDetail',
        query: { row: JSON.stringify(row) }
      })
    },
    handleEdit(bool, row) {
      console.log(bool, row)
      this.$router.push({
        name: 'evaluateCreatePlan',
        params: { type: 'edit', row }
      })
    }
  }
}
</script>

<style scoped>
#box {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}
.el-form-item {
  margin-bottom: 10px;
}
</style>